package com.yyfax.pay.depository.controller.web;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.yyfax.framework.common.Pair;
import com.yyfax.pay.common.exception.PayException;
import com.yyfax.pay.common.exception.PayExceptionCode;
import com.yyfax.pay.common.model.to.YYPayFormReq;
import com.yyfax.pay.common.model.vo.BackResultFormVO;
import com.yyfax.pay.common.model.vo.RedirectFormVO;
import com.yyfax.pay.common.model.vo.ValidResult;
import com.yyfax.pay.common.util.ValidationUtil;
import com.yyfax.pay.depository.service.inner.AccountInnerService;
import com.yyfax.pay.depository.vo.FuiouChangePhoneVO;
import com.yyfax.pay.order.service.PayCommonService;
import com.yyfax.pay.third.model.to.fuioudeposit.FuiouChangePhoneBackReq;
import com.yyfax.pay.third.model.to.fuioudeposit.FuiouChangePhoneFormVO;
import com.yyfax.pay.vo.depository.ChangePhoneReq;

@Controller
public class AccountWebController {

	@Resource
	private ValidationUtil validationUtil;

	@Resource
	private AccountInnerService accountInnerService;

	@Resource
	private PayCommonService payCommonService;

	/**
	 * 更换手机网关模式
	 * 
	 * @author buyi
	 * @date 2017-09-09 14:25:09
	 * @since v2.0.0
	 * @param req
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/web/fuiou/account/change_phone")
	public String changePhone(YYPayFormReq req, Model model) {
		// 校验绑定参数
		Pair<ValidResult, ChangePhoneReq> pair = validationUtil.validateForWeb(req, ChangePhoneReq.class);
		if (!pair.first().isOk()) {
			throw PayExceptionCode.ERROR_ILL_PARAMETER.expMsg(pair.first().getErrMsg());
		}

		// 校验参数
		boolean isSign = payCommonService.validSign(req.getMerchantCode(), pair.second(), req.getSign());
		if (!isSign) {
			// 如果验签不通过，抛出异常，由异常处理统一跳转页面
			throw PayExceptionCode.ERROR_SIGN_FAIL.exp();
		}

		FuiouChangePhoneVO vo = accountInnerService.saveChangePhone(req.getMerchantCode(), pair.second());
		if (vo.isResult()) {
			// 如果订单已经结果，抛出异常，由异常处理统一跳转页面
			throw new PayException(vo.getPo().getRspCode(), vo.getPo().getRspMsg());
		}

		// 封装form表单
		RedirectFormVO<FuiouChangePhoneFormVO> routeFormVO = accountInnerService.getChangePhoneForm(vo);
		model.addAttribute("url", routeFormVO.getUrl());
		model.addAttribute("form", JSON.toJSON(routeFormVO.getParams()));

		return "fuioudeposit/guide";
	}

	/**
	 * 更换手机页面回跳请求路径
	 * 
	 * @author buyi
	 * @date 2017-09-09 14:25:27
	 * @since v2.0.0
	 * @param merchantCode
	 * @param req
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/web/fuiou/account/change_phone/result/back/{merchantCode}")
	public String changePhonePageBack(@PathVariable("merchantCode") String merchantCode, FuiouChangePhoneBackReq req, Model model) {
		// 解析请求合法性
		FuiouChangePhoneVO vo = accountInnerService.parseChangePhoneBackResult(merchantCode, req);

		// 封装同步跳转表单
		RedirectFormVO<BackResultFormVO> routeFormVO = accountInnerService.getChangePhoneRouteForm(vo.getPo());
		model.addAttribute("url", routeFormVO.getUrl());
		model.addAttribute("form", JSON.toJSON(routeFormVO.getParams()));

		return "fuioudeposit/guide";
	}

}
